""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"authorinfo settings
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
map     <F4>    :AuthorInfoDetect<cr>

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"UltiSnips settings
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if &rtp =~ 'UltiSnips'
    let g:UltiSnipsExpandTrigger="<tab>"
    let g:UltiSnipsJumpForwardTrigger="<c-j>"
    let g:UltiSnipsJumpBackwardTrigger="<c-k>"

    if g:is_win
        let g:UltiSnipsSnippetsDir="~/PluginConfig/UltiSnipsDir/"
        let g:UltiSnipsSnippetDirectories=["C:/home/PluginConfig/UltiSnipsDir/"]
    else
        let g:UltiSnipsSnippetsDir="~/vimrc/.config/nvim/ultisnips"
        let g:UltiSnipsSnippetDirectories=["~/vimrc/.config/nvim/ultisnips"]
    endif

    autocmd FileType snippets  setlocal expandtab
endif
"let g:UltiSnipsSnippetDirectories=["UltiSnipsDir"]

if &rtp =~ 'coc1.nvim'
    let g:coc_config_home = "~/.config/nvim"
    set updatetime=300
    set hidden
    
    inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm() : "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
    inoremap <expr><M-k> coc#pum#visible() ? coc#pum#prev(1) : "\<Up>"
    inoremap <expr><M-j> coc#pum#visible() ? coc#pum#next(1) : "\<Down>"
    nmap <silent> <space>gd <Plug>(coc-definition)
    " nmap <silent> <space>gy <Plug>(coc-type-definition)
    " nmap <silent> <space>gi <Plug>(coc-implementation)
    nmap <silent> <space>gr <Plug>(coc-references)
    nmap <silent> [g <Plug>(coc-diagnostic-prev)
    nmap <silent> ]g <Plug>(coc-diagnostic-next)

    call coc#add_extension(
                \ 'coc-calc', 
                \ 'coc-clangd', 
                \ 'coc-pyright', 
                \ 'coc-json', 
                \ 'coc-lists', 
                \ 'coc-marketplace',
                \ 'coc-pairs', 
                \ 'coc-snippets', 
                \ 'coc-syntax', 
                \ 'coc-tag', 
                \ 'coc-word'
                \ )

    hi link CocFloating DiffAdd

    nmap <space>ds  :vs<cr>:CocCommand clangd.switchSourceHeader<cr>

    nmap <space>cr      :CocRestart<cr>
    nmap <space>co      :CocOutline<cr>

    "大于100Kb禁用coc
    let g:trigger_size = 1024 * 1024 * 1

    augroup hugefile
        autocmd!
        autocmd BufReadPre *
                    \ let size = getfsize(expand('<afile>')) |
                    \ if (size > g:trigger_size) || (size == -2) |
                    \   echohl WarningMsg | echomsg 'WARNING: altering options for this huge file!' | echohl None |
                    \   exec 'CocDisable' |
                    \ else |
                    \   exec 'CocEnable' |
                    \ endif |
                    \ unlet size
    augroup END

    " function! s:show_documentation()
        " if (index(['vim','help', 'lua'], &filetype) >= 0)
            " execute 'h '.expand('<cword>')
        " elseif (coc#rpc#ready())
            " call CocActionAsync('doHover')
        " else
            " execute '!' . &keywordprg . " " . expand('<cword>')
        " endif
    " endfunction
    " nnoremap <silent> K :call <SID>show_documentation()<CR>
    nnoremap <silent> K :call ShowDocumentation()<CR>

    function! ShowDocumentation()
        if CocAction('hasProvider', 'hover')
            call CocActionAsync('doHover')
        else
            call feedkeys('K', 'in')
        endif
    endfunction

endif
